package net.jlibrary.student.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.student.action.vo.SpecialtyVo;
import net.jlibrary.student.service.ISpecialtyManager;

@RequestMapping("/student")
@RestController
public class SpecialtyAction {
	@Resource
	ISpecialtyManager specialtyMapper;

	@RequestMapping(value = "/specialtyView", method = { RequestMethod.GET })
	public ModelAndView specialtyView() {
		return new ModelAndView("/student/specialtyView");
	}

	@RequestMapping(value = "/specialtyJsonList", method = { RequestMethod.POST })
	public Pagination Lists(SpecialtyVo specialty, PaginationParam param) {
		Pagination pagination = specialtyMapper.listsPage(specialty, param);
		return pagination;

	}

	@RequestMapping(value = "/specialtyJsonDetail", method = { RequestMethod.GET })
	public SpecialtyVo detail(Long id) {
		SpecialtyVo specialtyVo = new SpecialtyVo();
		specialtyVo.setId(id);
		specialtyVo = specialtyMapper.get(specialtyVo);
		return specialtyVo;
	}

	@RequestMapping(value = "/specialtyJsonAddFormInit", method = { RequestMethod.GET })
	public SpecialtyVo addFormInit() {
		SpecialtyVo specialty = new SpecialtyVo();
		specialty.setValid(true);
		return specialty;
	}

	@RequestMapping(value = "/specialtyJsonSave", method = { RequestMethod.POST })
	public ModelMap save(@ModelAttribute("vm") SpecialtyVo vo) {
		specialtyMapper.save(vo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/specialtyJsonUpdate", method = { RequestMethod.POST })
	public ModelMap update(@ModelAttribute("vm") SpecialtyVo vo) {
		specialtyMapper.update(vo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/specialtyJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		SpecialtyVo specialtyVo = new SpecialtyVo();
		specialtyVo.setId(id);
		specialtyMapper.delete(specialtyVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/specialtyJsonData", method = { RequestMethod.GET })
	public List<ModelMap> specialtyJsonData() {
		List<SpecialtyVo> specialtyVos = specialtyMapper.lists(new SpecialtyVo());
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (SpecialtyVo specialtyVo : specialtyVos) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, specialtyVo.getPrimaryKey());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, specialtyVo.getText());
			maps.add(map);
		}
		return maps;
	}
}
